package net.minecraft.src;

import java.io.*;
import java.util.List;

public class LF_EntitySeat extends Entity
{
	boolean placedAlready = false;
	int type = 0;
	byte subType = 0;

	public LF_EntitySeat(World world)
	{
		super(world);
		setSize(1F,1F);
		yOffset = 0F;
		motionX = 0;
		motionY = 0;
		motionZ = 0;
	}

	public LF_EntitySeat(World world, double i, double j, double k)
	{
		this(world);
		setPosition(i,j,k);
		prevPosX = posX;
		prevPosY = posY;
		prevPosZ = posZ;
	}

	public LF_EntitySeat(World world, double i, double j, double k, int dmg)
	{
		this(world,i,j,k);
		type = dmg;
	}

	public LF_EntitySeat(World world, int i, int j, int k, int dmg)
	{
		this(world, i+0.5F, j, k+0.5F,dmg);
	}

	public AxisAlignedBB getEntityCheckBox(int x, int y, int z)
	{
		return AxisAlignedBB.getBoundingBox(blockX() + x,blockY() + y,blockZ() + z,
				blockX()+ 1 + x,blockY() + 1 + y,blockZ()+ 1 + z);
	}

	@Override
	public AxisAlignedBB getBoundingBox()
	{
		return AxisAlignedBB.getBoundingBox(blockX(), blockY(), blockZ(), blockX() + 1, blockY() + getMountedYOffset() + getColYOffset(), blockZ() + 1);
	}

	@Override
	public AxisAlignedBB getCollisionBox(Entity entity)
	{
		return AxisAlignedBB.getBoundingBox(blockX(), blockY(), blockZ(), blockX() + 1, blockY() + getMountedYOffset(), blockZ() + 1);
	}

	@Override
	public void onEntityUpdate()
	{
	}

	@Override
	public boolean interact(EntityPlayer player)
	{
		if(player.ridingEntity == null)
		{
			if(riddenByEntity == null)
			{
				if(!ModLoader.getMinecraftInstance().isMultiplayerWorld())
				{
					player.setLocationAndAngles(posX, posY, posZ, rotationYaw, 0.0F);
					player.mountEntity(this);
				}
				
			}
		}
		else
		{
			if(player.ridingEntity == this && riddenByEntity == player)
			{
				player.ridingEntity = null;
				riddenByEntity = null;
				player.setPosition(player.posX, player.posY + getMountedYOffset(), player.posZ);
			}
		}

		return true;
	}

	public void setSubType()
	{
		if(type == 0)
		{
			int xOff = 0;
			int zOff = 0;
			boolean isBehind = false;
			boolean isFront = false;
			boolean isLeft = false;
			boolean isRight = false;
			float rotation = rotationYaw;

			while(rotation < 0 || rotation >= 360)
			{
				if(rotation < 0)
					rotation += 360;
				if(rotation >= 360)
					rotation -= 360;
			}

			int thisRot = Math.round(rotation/ 90f);

			List<LF_EntitySeat> entities;
			if(thisRot == 0)
			{
				xOff = -1;
				zOff = 0;
			}

			if(thisRot == 1)
			{
				xOff = 0;
				zOff = -1;
			}

			if(thisRot == 2)
			{
				xOff = 1;
				zOff = 0;
			}

			if(thisRot == 3) {
				xOff = 0;
				zOff = 1;
			}

			int entRot;
			float entRotation;
			int plusAmount;
			int subAmount;
			entities = worldObj.getEntitiesWithinAABB(LF_EntitySeat.class, getEntityCheckBox(xOff, 0, zOff));

			if(entities.size() != 0)
			{
				if(entities.get(0).type == 0)
				{
					entRot = Math.round(entities.get(0).rotationYaw / 90F);
					while(entRot < 0 || entRot >= 4)
					{
						if(entRot < 0)
							entRot += 4;
						if(entRot >= 4)
							entRot -= 4;
					}

					if(thisRot == 3)
						plusAmount = -4;
					else
						plusAmount = 1;

					if(thisRot == 0)
						subAmount = 3;
					else
						subAmount = -1;

					if(entRot == thisRot || entRot == (thisRot + plusAmount) || entRot == (thisRot + subAmount))
					{
						// TODO Apparently stopped working here, as the line wasn't even finished, inserted 9999 just to complete it for now
						if(entities.get(0).subType != 9999)
						{
						}
					}
				}
			}
		}
	}
	
	@Override
	public double getMountedYOffset()
	{
		switch(type)
		{
			case  0:
				return 0.4375D;
			case  1:
				return 1.0F;
			case 16:
				return 0.1875F;
			default:
				return 0D;
		}
	}

	public double getColYOffset()
	{
		switch(type)
		{
			case 16:
				return 0.0625D;
			default:
				return 0D;
		}
	}

	public double blockX()
	{
		return posX - 0.5F;
	}

	public double blockY()
	{
		return posY;
	}

	public double blockZ()
	{
		return posZ - 0.5F;
	}

	@Override
	public void setLocationAndAngles(double d, double d1, double d2, float f, float f1)
	{
	}

	@Override
	public void setPositionAndRotation(double d, double d1, double d2, float f, float f1)
	{
	}

	@Override
	public void setPositionAndRotation2(double d, double d1, double d2, float f, float f1, int i)
	{
	}

	@Override
	public void setPosition(double d, double d1, double d2)
	{
		if(!placedAlready)
		{
			super.setPosition(d,d1,d2);
			placedAlready = true;
		}
	}

	@Override
	public boolean canBeCollidedWith()
	{
		return true;
	}

	@Override
	protected void entityInit()
	{
	}

	@Override
	public void writeEntityToNBT(NBTTagCompound nbttagcompound)
	{
		nbttagcompound.setInteger("type", type);
		nbttagcompound.setByte("subtype", subType);
	}

	@Override
	public void readEntityFromNBT(NBTTagCompound nbttagcompound)
	{
		type = nbttagcompound.getInteger("type");
		subType = nbttagcompound.getByte("subtype");
	}
}
